package com.gms.controller.equipmanger;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gms.entity.Repair.Repair;
import com.gms.entity.equipment.Equipment;
import com.gms.entity.equipment.Equipmentrent;
import com.gms.entity.user.User;
import com.gms.service.equipmanager.EquipmentService;
import com.gms.service.equipmanager.EquipmentrentService;
import com.gms.service.repairmanager.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jianghai
 * @since 2021-06-14
 */
@Controller
@RequestMapping("/equipment")
public class EquipmentController {

    String path = "c:/ProgramData/file";
    //图片保存目录
    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private EquipmentrentService equipmentrentService;

    @Autowired
    private RepairService repairService;

    //显示器材
    @RequestMapping("/showEquipment")
    public String showEquipment(Model model, HttpSession session) {
        Page<Equipment> page = equipmentService.select_page(1);//展示第一个页面
        model.addAttribute("page",page);
        User user = (User) session.getAttribute("user");
        if(!user.getPermission().equals("2")){
            return "userequipment";
        }
        return "equipment";
    }

    //添加器材页面
    @RequestMapping("/toinsertEquipment")
    public String queryEquipment() {
        return "equipAdd";
    }

    //添加器材
    @RequestMapping("/insertEquipment")
    public String addEquipment(Equipment equipment, MultipartFile pic, Model model) {
        //图片处理
        try {
            if (pic.getSize()>0) {
                File file = new File(path, pic.getOriginalFilename());
                if (file.exists()) {
                    file.delete();
                }
                File file1 = new File(path, pic.getOriginalFilename());
                pic.transferTo(file1);
                equipment.setEquipPic(pic.getOriginalFilename());
                equipment.setEquipType(pic.getContentType());
                //equipment.setBuydate(pic.getContentType());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        equipment.setStatus("1");
        equipmentService.addEquipment(equipment);
        return "redirect:/equipment/query";
    }

    //删除器材
    @RequestMapping("/deleteEquipment")
    public String deleteEquipment(Integer eId) {
        Equipment equipment = equipmentService.queryById(eId);
        equipmentrentService.delete_orders(equipment.getEtypename()); //删除器材的名字
        //equipmentService.delEquipment(equipment.getEid());
        //图片
        File file = new File(path, equipment.getEquipPic());
        if (file.exists()) {
            file.delete();
        }
        equipmentService.delEquipment(eId);
        return "redirect:/equipment/query";
    }

    //到器材编辑页面
    @RequestMapping("/toAlter")
    public String toAlterEquipment(Integer eId, Model model) {
        Equipment equipment = equipmentService.queryById(eId);
        model.addAttribute("equip",equipment);
        return "equipUpdate";
    }

    //修改器材
    @RequestMapping("/alterEquipment")
    public String alterEquipment(Equipment equipment, MultipartFile pic) {
        //创建文件
        //System.out.println(pic.getSize());
        try {
            if (pic.getSize()>0) {
                File file = new File(path, pic.getOriginalFilename());
                if (file.exists()) {
                    file.delete();
                }
                File file1 = new File(path, pic.getOriginalFilename());
                pic.transferTo(file1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("获取修改后的价格");
        System.out.println(equipment.getEfee());
        System.out.println("获取修改后的名字");
        System.out.println(equipment.getEtypename());
        equipment.setEquipPic(pic.getOriginalFilename());
        equipment.setEquipType(pic.getContentType());

        equipmentService.alterEquipment(equipment);
        return "redirect:/equipment/query";
    }

    //根据器材名字和器材种类id查询
    @RequestMapping("/query")
    public String queryEquipment(@Nullable String etypename,
                                 @Nullable Integer etypeid,
                                 @Nullable Integer pageNum,
                                 Model model,
                                 HttpSession session) {
        if (pageNum == null) pageNum = 1;
        if (!StringUtils.isEmpty(etypename)) {
            model.addAttribute("etypename",etypename);
        }
        if (etypeid != null) {
            model.addAttribute("etypeid",etypeid);
        }
        Page<Equipment> page = equipmentService.query_equip(etypename,etypeid,pageNum);
        model.addAttribute("page",page);
        User user = (User) session.getAttribute("user");
        if(!user.getPermission().equals("2")){
            return "userequipment";
        }
        return "equipment";
    }

    //跳转到租借页面
    @RequestMapping("/toinsertRentOrder")
    public String toinsertOrder(Integer eId, Model model,HttpSession session) {
        Equipment equipment = equipmentService.queryById(eId);
        model.addAttribute("etypename", equipment.getEtypename());
        model.addAttribute("equippic",equipment.getEquipPic());
        model.addAttribute("eventid",equipment.getEventid());
        User user = (User) session.getAttribute("user");
        if(!user.getPermission().equals("2")){
            return "userequipRent";
        }
        return "equipRent";
    }

    //租借器材操作
    @RequestMapping("/insertRentOrder")
    public String InsertOrder(Equipmentrent equipmentrent, Model model) {
        equipmentrentService.addEquipmentOrder(equipmentrent);
        System.out.println(equipmentrent.getEtypename());
        Equipment equipment = equipmentService.queryByName(equipmentrent.getEtypename());
        equipment.setStatus("0");   //表示租界中
        equipmentService.alterEquipment(equipment);
        return "redirect:/equipment/query";
    }

    //显示租借中的列表
    @RequestMapping("/order")
    public String OrderList(@Nullable Integer uId,
                            @Nullable String userName,
                            @Nullable Integer pageNum,
                            Model model) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (!StringUtils.isEmpty(userName)) {
            model.addAttribute("username",userName);
        }
        if (uId != null) {
            model.addAttribute("uId",uId);
        }
        Page<Equipmentrent> page = equipmentrentService.pageRent(pageNum, userName, uId);
        model.addAttribute("page",page);
        return "Equiporder";
    }

    //显示所有器材，包括已回收器材
    @RequestMapping("/allorder")
    public String AllOrderList(@Nullable Integer uId,
                               @Nullable String userName,
                               @Nullable Integer pageNum,
                               Model model) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (!StringUtils.isEmpty(userName)) {
            model.addAttribute("username",userName);
        }
        if (uId != null) {
            model.addAttribute("uId",uId);
        }
        Page<Equipmentrent> page = equipmentrentService.pageAllOrder(pageNum, userName, uId);
        model.addAttribute("page",page);
        return "EquipAllorder";
    }

    @RequestMapping("/torecovery")
    public String torecovery(Integer eId, Model model) {
        Equipment equipment = equipmentService.queryById(eId);
        model.addAttribute("etypename",equipment.getEtypename());
        return "Repair";
    }

    @RequestMapping("/recovery")
    public String recovery(Repair repair) {
        Equipment equipment = equipmentService.queryByName(repair.getEtypename());
        equipment.setStatus("待维修");
        equipmentService.alterEquipment(equipment);
        repairService.insertRepair(repair);
        return "redirect:/equipment/query";
    }

    @RequestMapping("/recover")
    public String recover(Integer erentid) {
        Equipmentrent equipmentrent = equipmentrentService.selectOneEquipmentOrder(erentid);
        Equipment equipment = equipmentService.queryByName(equipmentrent.getEtypename());
        equipment.setStatus("1");   //未租借
        equipmentService.alterEquipment(equipment);
        equipmentrentService.alterEquipmentOrder(erentid);
        return "redirect:/equipment/query";
    }

}
